package com.JZ.QM.service.impl;

import com.JZ.QM.common.dao.TotalEarningDetailDao;
import com.JZ.QM.common.dao.TotalEarningRecordDao;
import com.JZ.QM.common.dao.UserAccountDao;
import com.JZ.QM.common.entity.ComEarningRecordEntity;
import com.JZ.QM.common.entity.TotalEarningDetailEntity;
import com.JZ.QM.common.entity.TotalEarningRecordEntity;
import com.JZ.QM.common.entity.UserAccountEntity;
import com.JZ.QM.common.utils.PageUtils;
import com.JZ.QM.common.utils.Query;
import com.JZ.QM.service.TotalEarningRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: cyc
 * @email cyc@gmail.com
 * @date: 2020/7/8 14:31
 */
@Service("totalEarningRecord")
public class TotalEarningRecordServiceImpl extends ServiceImpl<TotalEarningRecordDao, TotalEarningRecordEntity> implements TotalEarningRecordService {

    @Resource
    TotalEarningDetailDao totalEarningDetailDao;
    @Resource
    TotalEarningRecordDao totalEarningRecordDao;
    @Resource
    UserAccountDao userAccountDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) throws Exception {
        IPage<TotalEarningRecordEntity> page = this.page(
                new Query<TotalEarningRecordEntity>().getPage(params),
                new QueryWrapper<TotalEarningRecordEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 计算更新用户的总收益
     * @return
     */
    @Override
    public Boolean caculateTotalEarning() {
        //累计收益明细列表
        ArrayList<TotalEarningDetailEntity> totalEarningDetailEntities=new ArrayList<>();
        TotalEarningDetailEntity totalEarningDetailEntity=new TotalEarningDetailEntity();
        TotalEarningRecordEntity totalEarningRecordEntity=new TotalEarningRecordEntity();
        UserAccountEntity userAccountEntity=new UserAccountEntity();
        //定义时间的格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");//设置日期格式
        //定义日历时间
        Calendar c = Calendar.getInstance();
        //前一天时间
        Date start=new Date();
        //前一天日期格式转换
        String TotalUpdateDate="";
        BigDecimal totalEarning= new BigDecimal(0);
        BigDecimal totalEarningRadio=new BigDecimal(0);

        //取得前一天日期
        c.add(Calendar.DATE, -1);
        start = c.getTime();
        TotalUpdateDate = dateFormat.format(start);
        //获取所有用户收益情况
        totalEarningDetailEntities=totalEarningDetailDao.getTotalEarningDetailList(TotalUpdateDate);
        System.out.println("累计为"+totalEarningDetailEntities);
        for (Iterator<TotalEarningDetailEntity> it = totalEarningDetailEntities.iterator(); it.hasNext(); ) {
            totalEarningDetailEntity = it.next();
            totalEarning=totalEarningDetailEntity.getEarningAmount().add(totalEarningDetailEntity.getAccountAmoount());
            if(totalEarningDetailEntity.getAccountAmoount().compareTo(new BigDecimal(0))!=0){
                totalEarningRadio=totalEarningDetailEntity.getEarningAmount().divide(totalEarningDetailEntity.getAccountAmoount(),5,BigDecimal.ROUND_HALF_UP);
            } else {
                totalEarningRadio=new BigDecimal(0);
            }
            //创建累计收益记录实体，便于插入至收益表中
            totalEarningRecordEntity.setAccountId(totalEarningDetailEntity.getAccountId());
            totalEarningRecordEntity.setEarningAmount(totalEarningDetailEntity.getEarningAmount());
            totalEarningRecordEntity.setEarningRate(totalEarningRadio);
            totalEarningRecordEntity.setUserId(totalEarningDetailEntity.getUserId());
            totalEarningDetailEntity.setTradeDate(TotalUpdateDate);
            System.out.println("正在运行");
            //将对应的用户收益插入到表中
            totalEarningRecordDao.insert(totalEarningRecordEntity);
            userAccountEntity.setAccountAmoount(totalEarning);
            userAccountEntity.setAccountId(totalEarningDetailEntity.getAccountId());
            //更新用户的平台账号的金额
            userAccountDao.updateById(userAccountEntity);
            System.out.println("正在更新");
        }
        return true;
    }
}
